package com.csu.express.controller.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.exception.WebException;
import com.csu.common.vo.ResultVo;
import com.csu.express.constants.ErrorCode;
import com.csu.express.pojo.DriverLocationPojo;
import com.csu.express.pojo.DriverPojo;
import com.csu.express.service.DriverService;
import com.csu.express.service.OrderService;
import com.csu.express.vo.DriverVo;
import com.csu.express.vo.OrderDriverVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/** 
 * @项目名称：express
 * @类名称：DriverController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月21日 下午3:32:31
 * @company:
 */
@Controller
@RequestMapping("/driver")
@Api(value = "driver", description = "司机APP服务接口")
public class DriverController
{
    @Autowired
    private DriverService driverService;

    @Autowired
    private OrderService orderService;

    @ResponseBody
    @ApiOperation(value = "司机登录", notes = "登录校验")
    @RequestMapping(value = "/producer/login", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<DriverPojo> driverLogin(HttpServletRequest request,
            @ApiParam(value = "电话号码") @RequestParam(value = "cellPhone", required = true) String cellPhone,
            @ApiParam(value = "密码") @RequestParam(value = "passwd", required = true) String passwd) throws WebException
    {
        if (cellPhone == null || "".equals(cellPhone) || passwd == null || "".equals(passwd))
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        ResultVo<DriverPojo> result = new ResultVo<DriverPojo>();

        // 登录操作
        DriverPojo driver = driverService.login(cellPhone, passwd);

        result.setData(driver);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查看司机信息", notes = "顾客查看司机信息")
    @RequestMapping(value = "/info/{driverSn}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<DriverVo> driverInfo(HttpServletRequest request, @ApiParam(value = "司机编号") @PathVariable(value = "driverSn") String driverSn)
            throws WebException
    {

        if (driverSn == null || "".equals(driverSn))
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        ResultVo<DriverVo> result = new ResultVo<>();

        // 获取个人信息
        DriverVo driver = driverService.getDriverInfoBySn(driverSn);

        result.setData(driver);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "司机上报位置", notes = "司机上报位置信息")
    @RequestMapping(value = "/info/updateLocation", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public void updateLocation(HttpServletRequest request, @RequestBody DriverLocationPojo driverLocationPojo) throws WebException
    {

        if (driverLocationPojo == null || driverLocationPojo.getDriverSn() == null || driverLocationPojo.getLongitude() == null
                || driverLocationPojo.getLatitude() == null)
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        // 更新司机位置缓存信息
        driverService.updateLocation(driverLocationPojo);
    }

    @ResponseBody
    @ApiOperation(value = "获取司机位置信息", notes = "获取司机位置信息")
    @RequestMapping(value = "/info/getLocation/{driverSn}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<DriverLocationPojo> getLocation(HttpServletRequest request,
            @ApiParam(value = "司机编号") @PathVariable(value = "driverSn") String driverSn) throws WebException
    {

        if (driverSn == null || "".equals(driverSn))
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        ResultVo<DriverLocationPojo> result = new ResultVo<>();

        // 获取司机位置信息
        DriverLocationPojo driverLocation = driverService.getDriverLocation(driverSn);

        result.setData(driverLocation);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "修改服务信息", notes = "司机修改服务信息（包括配送模式、接单状态）")
    @RequestMapping(value = "/info/update", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> updateStatus(HttpServletRequest request, @RequestBody DriverVo driverVo) throws WebException
    {

        if (driverVo == null || driverVo.getDriverSn() == null)
        {
            // 参数不能为空
            throw new WebException(ErrorCode.ParamNullCode, ErrorCode.ParamNullMessgae);
        }

        // 更新司机服务信息
        int result = driverService.updateServiceInfo(driverVo);
        ResultVo<Integer> resultVo = new ResultVo<>();
        resultVo.setData(result);
        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询订单列表", notes = "查询订单列表")
    @RequestMapping(value = "getOrderList/{driverId}/{queryStatus}/{begin}/{size}", method = RequestMethod.GET, produces = {
            "application/json; charset=utf-8" })
    public ResultVo<List<OrderDriverVo>> getOrderList(HttpServletRequest request,
            @ApiParam(value = "司机id") @PathVariable(value = "driverId") Integer driverId,
            @ApiParam(value = "查询指定状态：1全部；2取货中 ；3送货中 ；4返回中；5已结束  ") @PathVariable(value = "queryStatus") Integer queryStatus,
            @ApiParam(value = "查询开始位置，从0开始") @PathVariable(value = "begin") Integer begin,
            @ApiParam(value = "查询条数") @PathVariable(value = "size") int size) throws WebException
    {
        ResultVo<List<OrderDriverVo>> result = new ResultVo<List<OrderDriverVo>>();
        List<OrderDriverVo> orderDriverVos = orderService.getOrderListByDriverId(driverId, begin, size, queryStatus);
        result.setData(orderDriverVos);
        return result;
    }

}
